package cn.ekuma.epos.security.dao;

import java.util.Date;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

import cn.ekuma.data.dao.BaseDAO;
import cn.ekuma.epos.security.AppLock;
import cn.ekuma.epos.security.db.table.I_AppLock;

public class AppLockDAO extends BaseDAO<AppLock>{

	public AppLockDAO(I_Session s) {
		super(s);
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s,
                "APP_LOCK",
                new Field[]{
                   new Field(I_AppLock.ID,Datas.STRING, Formats.STRING),
                   new Field(I_AppLock.ASKFOR,Datas.STRING, Formats.STRING),
                   new Field(I_AppLock.TARGER,Datas.STRING, Formats.STRING),
                   new Field(I_AppLock.TARGERID,Datas.STRING, Formats.STRING),
                   new Field(I_AppLock.MEMO,Datas.STRING, Formats.STRING),
                   new Field(I_AppLock.LOCKTIME,Datas.TIMESTAMP, Formats.TIMESTAMP) 
                 },
                new int[]{0}
                );
	}

	@Override
	public void writeInsertValues(DataWrite dp, AppLock obj)
			throws BasicException {
		dp.setString(1,obj.getKey());
		dp.setString(2,obj.getAskFor());
		dp.setString(3,obj.getTarger());
		dp.setString(4,obj.getTargetId());
		dp.setString(5,obj.getMemo());
		dp.setTimestamp(6,obj.getLockTime());
		
	}

	@Override
	public Class getSuportClass() {
		return AppLock.class;
	}

	
	public AppLock readValues(DataRead dr, AppLock obj) throws BasicException {
		if(obj==null)
		  obj=new AppLock();
		obj.setKey(dr.getString(1));
		obj.setAskFor(dr.getString(2));
		obj.setTarger(dr.getString(3));
		obj.setTargetId(dr.getString(4));
		obj.setMemo(dr.getString(5));
		obj.setLockTime(dr.getTimestamp(6));
		return obj;
	}
	
	
	
	public boolean lock(String askFor, String target, String targetId,
			String memo) {
		AppLock obj=new AppLock();
		obj.setAskFor(askFor);
		obj.setTarger(target);
		obj.setTargetId(targetId);
		obj.setMemo(memo);
		obj.setLockTime(new Date());
		try {
			this.insert(obj);
			return true;
		} catch (BasicException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	
	public boolean unlock(final String askFor, final String target, final String targetId) {
		
		 try {
			new StaticSentence(s, getTableDefinition().getDeleteSQL(new String[]{I_AppLock.ASKFOR,
			                                                          I_AppLock.TARGER,
			                                                          I_AppLock.TARGERID})
			            ,SerializerWriteParams.INSTANCE).exec(new DataParams() {
							@Override
							public void writeValues() throws BasicException {
								setString(1,askFor);
								setString(2,target);
								setString(3,targetId);
							}
			            });
			return true;
		} catch (BasicException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	
	public boolean unlockAllbyAskToTarget(final String askFor, final String target) {
		 try {
				new StaticSentence(s, getTableDefinition().getDeleteSQL(new String[]{I_AppLock.ASKFOR,
				                                                          I_AppLock.TARGER})
				            ,SerializerWriteParams.INSTANCE).exec(new DataParams() {
								@Override
								public void writeValues() throws BasicException {
									setString(1,askFor);
									setString(2,target);
									
								}
				            });
				return true;
			} catch (BasicException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return false;
	}

	
	public boolean unlockAllbyAsk(final String askFor) {
		 try {
				new StaticSentence(s, getTableDefinition().getDeleteSQL(new String[]{I_AppLock.ASKFOR})
				            ,SerializerWriteParams.INSTANCE).exec(new DataParams() {
								@Override
								public void writeValues() throws BasicException {
									setString(1,askFor);
								}
				            });
				return true;
			} catch (BasicException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return false;
	}
}
